package hd.util.activity;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ViewSwitcher;

import hd.util.R;

/**
 * TitleBarActivity拥有titleBar，可以很自由的进行定制
 * <p/>
 * Created by Haidy on 14/12/21.
 */
public class TitleBarActivity extends BaseActivity {

    private RelativeLayout titleBar;
    private LinearLayout bodyView;
    private ViewGroup loadingView;
    private ViewSwitcher switcher;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hindActionBar();
        setContentView(view = View.inflate(this, R.layout.default_view, null));
        titleBar = (RelativeLayout) findViewById(R.id.title_bar);
        bodyView = (LinearLayout) findViewById(R.id.body_view);
        loadingView = (ViewGroup) findViewById(R.id.loading_view);
        switcher = (ViewSwitcher) findViewById(R.id.sw_view);
    }

    /**
     * 设置TitleBar
     *
     * @param layout layout
     */
    public void setTitleBar(int layout) {
        setTitleBar(View.inflate(this, layout, null));
    }

    /**
     * 设置Titlebar
     *
     * @param v view
     */
    public void setTitleBar(View v) {
        this.titleBar.removeAllViews();
        v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        this.titleBar.addView(v);
    }

    /**
     * 设置ContentView
     *
     * @param layout layout
     */
    public void setBody(int layout) {
        setBody(View.inflate(this, layout, null));
    }

    public void setBody(View view) {
        bodyView.removeAllViews();
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        bodyView.addView(view);
    }

    public void showLoadingView() {
        switcher.setDisplayedChild(1);
    }

    public void showBodyView() {
        switcher.setDisplayedChild(0);
    }

    /**
     * 获取LoadingView
     *
     * @return view
     */
    public ViewGroup getLoadingView() {
        return loadingView;
    }

    /**
     * 设置LoadingView
     *
     * @param layout loadingView
     */
    public void setLoadingView(int layout) {
        setLoadingView(View.inflate(this, layout, null));
    }

    /**
     * 设置LoadingView
     *
     * @param view view
     */
    public void setLoadingView(View view) {
        loadingView.removeAllViews();
        view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        loadingView.addView(view);
    }

    /**
     * 获取TitleBar
     *
     * @return titleBar
     */
    public RelativeLayout getTitleBar() {
        return titleBar;
    }

    /**
     * 设置TitleBarColor
     *
     * @param color color
     */
    public void setTitleBarColor(int color) {
        titleBar.setBackgroundColor(color);
    }

    /**
     * BodyView
     *
     * @return view
     */
    public LinearLayout getBodyView() {
        return bodyView;
    }

    /**
     * 隐藏TitleBar
     */
    public void hideTitleBar() {
        titleBar.setVisibility(View.GONE);
        findViewById(R.id.title_bar_shadow).setVisibility(View.GONE);
    }

    /**
     * 显示标题栏
     */
    public void showTitleBar() {
        titleBar.setVisibility(View.VISIBLE);
        findViewById(R.id.title_bar_shadow).setVisibility(View.VISIBLE);
    }
}
